#!/bin/sh

. /opt/truecl/etc/clenv.sh

user=${1:-root}
h=`hostname`

r=`lha_stat --daemons -a echo 2>&1 | 
	perl -ne 'BEGIN {@D=qw(junk req cluster network sync lock heartbeat status monitor);}
		if($. > 1) {
			chomp;
			@F=split;
			$chost=$F[0];
			@down=();
			for my $i (2,4,5,6,7,8) {
				if($F[$i] eq "DOWN") {
					push @down,$D[$i];
				}
			}
			if(@down) {
				printf("Following daemons are down on \"%s\": %s\n",
					$chost,join(",",@down));
			}
		};'`

if [ -n "$r" ]
then
	if [ -x "/usr/bin/mailx" ]
	then
		echo "$r" | /usr/bin/mailx -s "Cluster daemons down" $user
		exit 0
	fi
	if [ -x "/usr/bin/mail" ]
	then
		echo "Subject: Cluster daemons down\n$r" | 
			/usr/bin/mail $user
	fi
fi

exit 0
